<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins" @checkboxRecords="checkboxRecords">
      <template #form="{ form }">
        <el-input
          v-model.trim="form.name"
          class="!w-[160px]"
          placeholder="属性名"
          clearable
        ></el-input>
        <el-select v-model="form.status" placeholder="状态" class="!w-[160px]" clearable>
          <el-option
            v-for="item in options.statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-select
          v-model="form.is_dangerous"
          placeholder="是否危险品"
          class="!w-[160px]"
          clearable
        >
          <el-option
            v-for="item in whetherOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </template>
      <template #toolbar_btns>
        <VButton auth="/goods/basics/logistics/add" type="primary" @click="open()"
          ><svg-icon icon-class="add_new" />新增</VButton
        >
        <VButton
          auth="/goods/basics/logistics/enable"
          :loading="loadings.enable"
          @click="handleStatus(1)"
        >
          <svg-icon class="mr-1 base-text-color" icon-class="state_finish_enable" />
          启用</VButton
        >
        <VButton
          auth="/goods/basics/logistics/disable"
          :loading="loadings.disable"
          @click="handleStatus(2)"
        >
          <svg-icon
            class="mr-1 text-red-500"
            icon-class="state_finish_cover"
          />禁用</VButton
        >
      </template>
    </VTable>
    <ComModal ref="addref" @query="query"></ComModal>
    <VLog
      ref="logRef"
      class="dialogPublic"
      :api="api.getLogisticsLog"
      options="id"
    ></VLog>
  </VPage>
</template>
<script setup lang="jsx" name="BasicsLogistics">
import ComModal from "./components/ComModal.vue";
import { ElMessage } from "element-plus";
import { getOption } from "@/api/index.js";
import VLog from "@/components/VLog";
import api from "./api";
import { whetherOptions } from "./data.js";

const tableRef = ref();
const addref = ref(null);

const records = ref([]);
let options = reactive({});

const loadings = reactive({
  enable: false,
  disable: false,
  del: false,
});

const tableOptins = reactive({
  formConfig: {
    data: {},
  },
  rowConfig: {
    height: 52,
  },
  columns: [
    { type: "checkbox", width: 50 },
    { field: "id", title: "ID", width: 100 },
    {
      field: "name",
      title: "属性名",
      minWidth: 100,
      slots: {
        default: ({ row }) => {
          return <div>{row.name}</div>;
        },
      },
    },
    {
      field: "is_dangerous_text",
      title: "是否危险品",
      minWidth: 120,
    },
    {
      field: "with_electricity_text",
      title: "是否含有电池",
      minWidth: 120,
    },
    {
      field: "status_cn",
      title: "状态",
      slots: {
        default: ({ row }) => {
          return (
            <div class="flex items-center">
              {row.status == 1 ? (
                <svg-icon class="mr-1 base-text-color" icon-class="state_finish_enable" />
              ) : (
                <svg-icon class="mr-1 text-red-500" icon-class="state_finish_cover" />
              )}
              {row.status_cn}
            </div>
          );
        },
      },
      width: 95,
    },

    { field: "comment", title: "备注", minWidth: 160 },
    { field: "create_user", title: "添加人", width: 160 },
    { field: "create_time", title: "添加时间", width: 160 },
    { field: "update_user", title: "修改人", width: 160 },
    { field: "update_time", title: "修改时间", width: 160 },
    {
      title: "操作",
      fixed: "right",
      width: 100,
      slots: {
        default: ({ row }) => {
          return (
            <div class="flex justify-start">
              <VButton
                auth="/goods/basics/logistics/edit"
                class="mr-4"
                type="primary"
                link
                underline={false}
                onClick={() => open(row)}
              >
                编辑
              </VButton>
              <el-link type="primary" underline={false} onClick={() => handleLog(row.id)}>
                日志
              </el-link>
            </div>
          );
        },
      },
    },
  ],
  proxyConfig: {
    props: {
      result: "data",
      total: "total",
    },
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page }),
    },
  },
});

onMounted(() => {
  fetchOption();
});
const query = () => {
  tableRef?.value.query();
};
const fetchOption = () => {
  // option不同接口判断
  getOption("currency").then((res) => {
    options = res.data;
  });
};
// 选中
const checkboxRecords = (arr) => {
  records.value = arr;
};
const logRef = ref();
const handleLog = (id) => {
  logRef.value.open(id);
};

const open = (data) => {
  nextTick(() => {
    addref.value.open(data);
  });
};
const isHandle = () => {
  const records = getSelectRow();
  return !records.length ? ElMessage.error("请先选择行!") && false : true;
};
//获取已选中的行
const getSelectRow = () => {
  return tableRef?.value.$table.getCheckboxRecords();
};

const handleStatus = (status) => {
  if (!isHandle()) return;
  const records = getSelectRow();
  status == 1 ? (loadings.enable = true) : (loadings.disable = true);
  api
    .updateStatus({ id: records.map((item) => item.id), status })
    .then((res) => {
      query();
      records.value = [];
      ElMessage.success(res.message);
    })
    .finally(() => {
      status == 1 ? (loadings.enable = false) : (loadings.disable = false);
    });
};
</script>
